//
//  PublishBuyMessageViewController.m
//  ShangPiBao
//
//  Created by chirui on 2017/10/25.
//  Copyright © 2017年 Shui. All rights reserved.
//

#import "PublishBuyMessageViewController.h"

@interface PublishBuyMessageViewController ()
{
    NSString * classIdStr;
}
@property (weak, nonatomic) IBOutlet UITextField *nameText;
@property (weak, nonatomic) IBOutlet UITextField *guigeText;
@property (weak, nonatomic) IBOutlet UITextField *numberText;
@property (weak, nonatomic) IBOutlet UIButton *keepBtn;
@property (weak, nonatomic) IBOutlet UILabel *className;

@end

@implementation PublishBuyMessageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    TitleView(@"发布求购信息");
    _keepBtn.layer.cornerRadius = 45/2.f;
    _keepBtn.layer.masksToBounds = YES;
    
//    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithIcon:@"icon_nav_return" target:self action:@selector(back)];
}
-(void)back{
    [self.navigationController popViewControllerAnimated:YES];
}


- (IBAction)chooseClassClick:(id)sender {
    
    NSArray * titleArray = @[@"五金机电",@"建筑材料",@"酒店用品",@"汽配汽用",@"文体办公",@"农林牧渔 ",@"服装鞋包",@"物流货运"];
    NSArray * titleIdArray = @[@"0,8",@"0,2",@"0,6",@"0,7",@"0,5",@"0,4",@"0,3",@"0,1"];
    
    zySheetPickerView * pickerView = [zySheetPickerView ZYSheetStringPickerWithTitle:titleArray andHeadTitle:@"分类" Andcall:^(zySheetPickerView *pickerView, NSString *choiceString) {
        _className.text = choiceString;
        
        [titleArray enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
            if ([obj isEqualToString:choiceString]) {
                NSLog(@"%@-索引%d 对应id = %@",obj, (int)idx,CLASS_ID_ARRAY[idx]);
                classIdStr =  titleIdArray[idx];
            }
        }];
        
        [pickerView dismissPicker];
        
    }];
    [pickerView show];
}

- (IBAction)keepBtnClick:(id)sender {
    
    if (_nameText.text==nil||_nameText.text.length<=0) {
        
        SHuiMBhudTextTimeStr(@"请输入产品介绍");
        return;
    }
    if (_guigeText.text==nil||_guigeText.text.length<=0) {
        
        SHuiMBhudTextTimeStr(@"请输入产品规格");
        return;
    }
    if (_numberText.text==nil||_numberText.text.length<=0) {
        
        SHuiMBhudTextTimeStr(@"请输入产品数量");
        return;
    }
    if (_className.text==nil||_className.text.length<=0) {
        
        SHuiMBhudTextTimeStr(@"请选择产品种类");
        return;
    }
    
    [self addGoodsRequest];
}

-(void)addGoodsRequest{
    
    NSString * md5ver = [SHJudgeTools md532BitLower:[NSString
                                                     stringWithFormat:@"%@api.shangpibao.com",USER_DEFAULTGet(@"phone")]];
    NSString * parStr = [NSString stringWithFormat:@"%@%@",address_url,add_shops_api];
    
    NSDictionary * dic = @{@"uphone":USER_DEFAULTGet(@"phone"),
                           @"uid":USER_DEFAULTGet(@"uid"),
                           @"ustatus":@"1",
                           @"utitle":_nameText.text,
                           @"usm":_guigeText.text,
                           @"unum":_numberText.text,
                           @"ucontent":@"",
                           @"uclass":classIdStr,
                           @"ver":md5ver};
    
    [PostNetWorking postNetWorkingStr:parStr andparameters:dic Success:^(id responseObject) {
        if ([responseObject[@"err"] intValue]==0) {
            
            
            SHuiMBhudTextTimeStr(@"添加成功");
            
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                
                [self.navigationController popViewControllerAnimated:YES];
            });
            
        }else{
            
            SHuiMBhudTextTimeStr(EorrMessageStr);
        }
        
    } failed:^(NSError *error) {
        SHuiMBHidleYes;
        
        SHuiMBhudTextTimeStr(@"网络错误，请稍后再试");
    }];
}




@end
